<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@include file="/include/common/gridbackhead.jsp"%>
<%!
private class PageAction extends BaseAction{
	ResultSet rs = null;
	StringBuilderEx sql = null;
	
	//准备action的环境变量 每次请次都会执行此方法 可在此方法中初如化一些变量
	protected void prepareActionEnv() throws Exception{
		sql = new StringBuilderEx();
	}
	
	protected void EndProcess() throws Exception{
		if(form.ationEq("save,delete")){
			onSearchRetrieveResult();
		}
	}
	
	//加載頁面默認數據
	protected boolean onLoadDefault() throws Exception{
		if(!form.isNullOrEmpty("pan_subpro_menusn")){
			rs = utils.executeQuery("info5","select * from pan..pan_subpro_menu where sn = '"+form.gpsv("pan_subpro_menusn")+"'");
			if(rs.next()){
				form.FromResultSet(rs, false);
				if("0".equalsIgnoreCase(form.gpsv("enab"))){
					ret.setWarningInfo("此單已取消!");
					page.getToolbar().disableAllButtonExcept("newnext");
				}
			}else{
				ret.setWarningInfo("此單已刪除,如需要請重新新增!");
				page.getToolbar().disableAllButtonExcept("newnext");
			}
			onSearchRetrieveResult();
		}
		return true ;
	}
	
	// 取出查询結果
	protected void onSearchRetrieveResult() throws Exception{		
		//已選IP
		if(!form.isNullOrEmpty("ips")){
			sql.Reset();
			sql.Append("\r\n select top 0 convert(nvarchar(20),'') as ip into #aa ");
			String ips[] = form.gpsv("ips").split(",");		
			for(int i=0;i<ips.length;i++){
				if(!StringHelper.IsNullOrEmpty(ips[i]))
					sql.Append("\r\n insert into #aa values('%1$s')",ips[i]);
			}
			sql.Append("\r\n  select a.ip,b.hr003name,b.hr003aname from #aa a left join pan..PAN_SUBPRO_APPIP b on a.ip = b.ip");
			sql.Append("\r\n drop table #aa");
			rs = utils.executeQuery("info5", sql.toString());
			ret.spov("rs1", rs);
		}
		
		//可選IP
		rs = utils.executeQuery("info5", "select distinct a.ip , a.hr003name,a.hr003aname "
			+" from pan..PAN_SUBPRO_APPIP a "
			+" order by a.hr003aname,a.hr003name");
		ret.spov("rs2", rs);
	}
	
	protected void doSave() throws Exception{
		sql.Reset();
		String ips[] = form.gpsvs("key2");
		int icode = 0; 
		String strIps = form.gpsv("ips");
		if(ips.length>0 || !form.isNullOrEmpty("ip2")){
			for(int i= 0;i<ips.length;i++){
				if((","+strIps+",").indexOf(","+ips[i]+",") == -1){
					if(strIps.length()>0){
						strIps += ",";
					}
					strIps += ips[i];
				}
			}
			
			if(!form.isNullOrEmpty("ip2")){
				if((","+strIps+",").indexOf(","+form.gpsv("ip2")+",") == -1){
					if(strIps.length()>0){
						strIps += ",";
					}
					strIps += form.gpsv("ip2");
				}
				form.RemoveParam("ip2");
			}
			
			if(!strIps.equals(form.gpsv("ips"))){
				//1.儲存
				CrudDataEntity crud = form.getCrudDataEntity("info5","pan..pan_subpro_menu","sn");
				crud.spsv("sn", form.gpsv("pan_subpro_menusn"));
				crud.setSpportSimple(true);
				crud.spsv("ips", strIps);
				crud.appendUpdateCols();
				icode = crud.save();
			}
		}
		
		if(icode > 0){
			form.spsv("ips", strIps);
			ret.setOkInfo("存儲成功!");
		}
	}
	
	protected void doDelete() throws Exception{
		sql.Reset();
		String ips[] = form.gpsvs("key1");
		int icode = 0; 
		String strIps = form.gpsv("ips");
		if(ips.length>0){
			strIps = ","+strIps+",";
			for(int i= 0;i<ips.length;i++){
				strIps = strIps.replaceAll(","+ips[i]+",", ",");
			}
			
			if(!strIps.equals(",")){
				strIps = strIps.substring(1,strIps.length()-1);
			}else{
				strIps ="";
			}
			
			if(!strIps.equals(form.gpsv("ips"))){
				//1.儲存
				CrudDataEntity crud = form.getCrudDataEntity("info5","pan..pan_subpro_menu","sn");
				crud.spsv("sn", form.gpsv("pan_subpro_menusn"));
				crud.setSpportSimple(true);
				crud.spsv("ips", strIps);
				crud.appendUpdateCols();
				icode = crud.save();
			}
		}
		
		if(icode > 0){
			form.spsv("ips", strIps);
			ret.setOkInfo("刪除成功!");
		}
	}

}
%>
<%@include file="/include/common/gridbackend.jsp"%>